package com.wu.family.feed;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.GalleryPhotoView;

import com.idle.view.CustomPhotoView;
import com.wu.family.MainGroupActivity;
import com.wu.family.R;
import com.wu.family.utils.BitmapTools;
import com.wu.family.utils.DisplayUtil;
import com.wu.family.utils.NetHelper;
import com.wu.family.utils.img.AbsBitmapMgr.ImageCallBack;
import com.wu.family.utils.ze.LoadBitmapMgr;
import com.wu.family.views.MyImageView;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
/**
 * @author zcw
 *
 */
public class FeedImageAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<String> photoUrls;
	private String isPublish = "";

	public FeedImageAdapter(Context context, ArrayList<String> photoUrls) {
		this.context = context;
		if (photoUrls.size() > 0) {
			isPublish = photoUrls.get(0);
			photoUrls.remove(0);
		}
		this.photoUrls = photoUrls;
	}

	@Override
	public int getCount() {
		return photoUrls!=null?photoUrls.size():0;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CustomPhotoView photoView = new CustomPhotoView(parent.getContext());
		photoView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		if (isPublish.equals("publish")) {
			Bitmap bitmap = BitmapTools.decode(photoUrls.get(position), 1);
			recycleLists.add(bitmap);
			photoView.setImageBitmap(bitmap);
		}else {
			LoadBitmapMgr.getInstance().loadBitmap(photoUrls.get(position), 
					photoView, null);
		}
		return photoView;
	}
	
	List<Bitmap> recycleLists = new ArrayList<Bitmap>();
	
	public void recycle() {
		Bitmap bitmap;
		for (int i = 0; i < recycleLists.size(); i++) {
			bitmap = recycleLists.get(i);
			if (bitmap != null && !bitmap.isRecycled()) {
				bitmap.recycle();
				bitmap = null;
			}
		}
		recycleLists = null;
		LoadBitmapMgr.getInstance().recycle(photoUrls);
		System.gc();
	}

	
}
